<?php

namespace App\AdminModule\Presenters;

use \App\Model\Dao\VideoComment,
    \Grido, 
    \Grido\Grid;
/**
 * Video comment presenter.
 */
class VideoCommentPresenter extends BasePresenter {
 
  /** @var \Kdyby\Doctrine\EntityManager @inject */
  public $em;
  
  private $video;
  
  /**
   * Delete comment
   * @param int $id
   */
  public function handleDelete($id){
    $comment = $this->em->getDao(VideoComment::getClassName())->findOneBy(array('id'=> $id));
    
    if($comment){
      // remove view
      $this->em->remove($comment);
      $this->em->flush();
    }
    
    $this->flashMessage('Shlédnutí bylo smazáno', 'success');
    $this->redirect('VideoComment:default');
  }
  
  /**
   * Comment grid
   * @param String $name
   * @return \Grido\Grid
   */
  public function createComponentVideoCommentGrid($name){
    $g = new Grid($this, $name);
    
    $qb = $this->em->createQueryBuilder()
      ->addSelect('c.id AS id')
      ->addSelect('c.content AS content')
      ->addSelect('v.webalize AS webalize')
      ->addSelect('u.login AS login')
      ->from(VideoComment::getClassName(), ' c')
      ->innerJoin('c.user', 'u')
      ->innerJoin('c.video', 'v');
    
    $comments =  $qb->getQuery()->getArrayResult();

    // Grido data
    $g->model = $comments;    
    
    $g->addColumnText('id', 'ID')
        ->setSortable()
        ->setFilterText();
    $g->addColumnText('content', 'Obsah')
        ->setSortable()
        ->setFilterText();
    $g->addColumnText('webalize', 'Název videa')
        ->setSortable()
        ->setFilterText();
    $g->addColumnText('login', 'Login uživatele')
        ->setSortable()
        ->setFilterText();

    // Create edit button
    $g->addActionHref('delete', 'Smazat', 'delete!')
      ->setIcon('remove')->setPrimaryKey('id');
    
    // Return grido
    $g->filterRenderType = Grido\Components\Filters\Filter::RENDER_INNER;
    $g->setExport();
    return $g;
  }
}
